/**
 * 提供页面相关方法的工具类
 */
class PageUtil {
    static getPageUrl() {
        return window.location.href;
    }
    static openUrlInNewTab(url) {
        // 都需要设置允许当前网站弹出窗口，如果没有设置允许，window.open返回null，但是从GM_openInTab返回的对象无法得知是否打开失败
        return window.open(url, '_blank');// 返回Window对象
        //return GM_openInTab(url, {active:true, insert:true, setParent:true});// 存在无法通过用户认证的问题
    }
    static isTabClosed(tab) {
        return tab ? tab.closed : true;// GM_openInTab()和window.open()返回的对象都有closed属性来表示是否关闭
    }
    static closePage() {
        if (navigator.userAgent.indexOf("Firefox") != -1 || navigator.userAgent.indexOf("Chrome") != -1) {
            window.location.href = "about:blank";
            window.close();
        } else {
            window.opener = null;
            window.open("", "_self");
            window.close();
        }
    }
    static refresh() { location.reload(); }
    static goToUrl(url) {
        if (location.href == url) {
            location.reload();
        } else {
            location.href = url;
        }
    }
    static scrollTo(x, y, behavior) {
        window.scrollTo({ left: x, top: y, behavior: behavior });
    }
    static getElementTop(element) {
        let actualTop = element.offsetTop;
        let parent = element.offsetParent;
        while (parent) {
            actualTop += parent.offsetTop;
            parent = parent.offsetParent;
        }
        return actualTop;
    }
}

// 导出PageUtil类
module.exports = PageUtil;